function doOnFormInit() {
	if (typeof editForm != 'object') return;
	var menu_type_combo = editForm.getCombo('menuType');
	menu_type_combo.addOption(parent.menuTypeComboData);
	menu_type_combo.setComboValue('');
	menu_type_combo.readonly(true);
	var status_combo = editForm.getCombo('status');
	status_combo.addOption(parent.statusComboData);
	status_combo.setComboValue('');
	status_combo.readonly(true);
	if(type=='add') {
		editForm.setItemValue("randomNum",parent.flag);
	}else{
		editForm.setItemValue("randomNum",parent.oneMenuInfo.id);
		doOnFormEditInit1();
	}
}

function doOnFormEditInit1() {
	if (!parent.oneMenuInfo || !parent.oneMenuInfo.id) return;
    //验证
	editForm.setItemValue("oldId", parent.oneMenuInfo.id);
	editForm.setItemValue("id", parent.oneMenuInfo.id);
	editForm.setItemValue('menuName', escape2Html(parent.oneMenuInfo.menu_name));
	editForm.setItemValue("parentId", parent.oneMenuInfo.parent_id);
	editForm.setItemValue("menuUrl", escape2Html(parent.oneMenuInfo.menu_url));
	editForm.setItemValue("menuType", parent.oneMenuInfo.menu_type);
	editForm.setItemValue("status", parent.oneMenuInfo.status);
	editForm.setItemValue("sortNum", escape2Html(parent.oneMenuInfo.sort_num));
	editForm.setItemValue("remark", escape2Html(parent.oneMenuInfo.remark));
	var imgSrc = parent.oneMenuInfo.picture_url;	//图片显示
	if(!imgSrc||imgSrc==''){
	} else {
		imgSrc = $($(imgSrc).html()).attr("src");
		$("#logoImg").attr("src",imgSrc);
	}

}

//初始化按钮form
function doOnButtonForm() {
	if (typeof (buttonForm) == "object") {
		buttonForm.attachEvent("onButtonClick", function(name, command) {
			switch (command) {
			case 'save':
				doSave();
				break;
			case 'cancle':
				doCancle();
				break;
			}
		});
	}
}

//校验新增/编辑form表单
function checkForm() {
	var formData = editForm.getFormData();
	if ( !$.trim(formData.id)) {
		$func.alert("菜单id不能为空!", "red");
		return false;
	}
	if ( !$.trim(formData.menuName)  ) {
		$func.alert("菜单名称不能为空!", "red");
		return false;
	}
	if (!$.trim(formData.parentId)   ) {
		$func.alert("菜单父id不能为空!", "red");
		return false;
	}
	if ( !$.trim(formData.menuUrl)  ) {
		$func.alert("菜单url不能为空!", "red");
		return false;
	}
	if ( !$.trim(formData.menuType)    ) {
		$func.alert("菜单类型不能为空!", "red");
		return false;
	}
	if ( !$.trim(formData.status)   ) {
		$func.alert("菜单状态不能为空!", "red");
		return false;
	}
    var sortNum= $.trim(formData.sortNum);
	if ( !sortNum ) {
		$func.alert("排序不能为空!", "red");
		return false;
	}
    if ( !sortNum.isInt (0,100000)){
        $func.alert("排序必须为数字,范围是1-99999", "red");
        return false;
    }
	return true;
}
//保存动作
function doSave(){
	if(!checkForm())return;
	//参数对象
	var formData = JSON.stringify(editForm.getFormData());
	var jsonObj=JSON.parse(formData);
	//图片校验
	var json = {'picWidth':18,'picHeight':18};	//非特殊pic要求
	$.ajaxFileUpload({
		url : basePath + 'common/checkPic',
		type : "post",
		secureuri : false,
		fileElementId : ["imgUrl"],
		dataType : "json",
		data : json,
		success : function (back) {
			if(!back || (!back.result && !back.code)){
				$func.alert(back.message?back.message:'图片检查出错','red');
				return false;
			}
			if(back.result){
				doCommit(jsonObj);
			} else {
				//图片尺寸不合法
				var confirmArr = back.message;
				$func.confirm({
					content : confirmArr,
					ok : function() {
						doCommit(jsonObj);
					}
				});
			}
		},
		error:function(xhr, textStatus, statusText){
			$func.alert('图片检查请求失败:'+textStatus+":"+statusText, "green");
		}
	});
}
//提交
function doCommit(jsonObj){
	var url= basePath + 'manageMenu/add';
	if(type == 'edit') url= basePath + 'manageMenu/update';
	$.ajaxFileUpload({
		url : url,
		type : "post",
		secureuri : false,
		fileElementId : ["imgUrl"],
		dataType : "json",
		data : jsonObj,
		success : function (data) {
			if(!data){
				$func.alert('保存出错','red');
				doCancle();
				return false;
			}
			if(!data.result){
				$func.alert(data.message?data.message:'保存出错','red');
				refreshImg();
				return false;
			}
			$func.alert("保存成功！", "green");
			parent.doRefresh();
			doCancle();
		},
		error:function(xhr, textStatus, statusText){
			$func.alert('保存请求失败:'+textStatus+":"+statusText, "green");
			refreshImg();
		}
	});
}
//取消动作
function doCancle() {
	parent.editWin.close();
}
//刷新图片
function refreshImg(){
	if(type=='edit'){
		//图片显示
		var imgSrc = parent.oneMenuInfo.picture_url;
		if(!imgSrc||imgSrc==''){
			$("#logoImg").attr("src",defaultImg);
		} else {
			imgSrc = $($(imgSrc).html()).attr("src");
			$("#logoImg").attr("src",imgSrc);
		}
	}else{
		$("#logoImg").attr("src",defaultImg);
	}
}
